package com.school.test.service.impl;


import com.school.common.core.exception.ServiceException;
import com.school.common.core.utils.StringUtils;
import com.school.test.domain.*;
import com.school.test.domain.vo.*;
import com.school.test.mapper.StatisticsVisitMapper;
import com.school.test.service.StatisticsVisitService;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


@Service
public class StatisticsVisitServiceImpl implements StatisticsVisitService {

    @Autowired
    private StatisticsVisitMapper statisticsVisitMapper;


    @Override
    public List<StatisticsVisit> selectInfo(ParamsVo paramsVo) {
        List<String> companyType = paramsVo.getCompanyType();
        if(StringUtils.isNotEmpty(paramsVo.getStartTime())){
            paramsVo.setStartTime(paramsVo.getStartTime() +" 00:00:00");
        }
        if(StringUtils.isNotEmpty(paramsVo.getEndTime())){
            paramsVo.setEndTime(paramsVo.getEndTime() +" 23:59:59");
        }
        String startTime = paramsVo.getStartTime();
        String endTime = paramsVo.getEndTime();
        return statisticsVisitMapper.selectInfo(companyType, startTime, endTime,paramsVo.getChannel());
    }


    @Override
    public List<StatisticsVisit> selectInfoAll(ParamsVo paramsVo) {
        List<String> companyType = paramsVo.getCompanyType();
        if(StringUtils.isNotEmpty(paramsVo.getStartTime())){
            paramsVo.setStartTime(paramsVo.getStartTime() +" 00:00:00");
        }
        if(StringUtils.isNotEmpty(paramsVo.getEndTime())){
            paramsVo.setEndTime(paramsVo.getEndTime() +" 23:59:59");
        }
        String startTime = paramsVo.getStartTime();
        String endTime = paramsVo.getEndTime();
        return statisticsVisitMapper.selectInfoAll(companyType, startTime, endTime,paramsVo.getChannel());
    }

    @Override
    public int addStatisticsVisit(StatisticsVisit statisticsVisit) {

        statisticsVisit.setCreate_time(statisticsVisit.getCreate_time()+ " 00:00:05");
        int row = statisticsVisitMapper.insertStatisticsVisit(statisticsVisit);
        return row;
    }


    @Override
    public int updateStatisticsVisit(StatisticsVisit statisticsVisit) {
        return statisticsVisitMapper.updateStatisticsVisit(statisticsVisit);
    }

    @Override
    public void deleteStatisticsVisitByIds(String id) {
        StatisticsVisit statisticsVisit = statisticsVisitMapper.selectStatisticsVisitById(id);
        if(null == statisticsVisit){
            throw new ServiceException(String.format("无法查询到统计数据"));
        }
        statisticsVisitMapper.deleteStatisticsVisitById(id);
    }

    @Override
    public StatisticsVisit selectInfoById(Long id) {
        StatisticsVisit statisticsVisit = statisticsVisitMapper.selectStatisticsVisitById(id.toString());
        return statisticsVisit;
    }

    @Override
    public List<StatisticsVisit> selectInfoByCompanyType(Long id ,int companyType,String createTime,String channel) {
        String startTime = createTime + " 00:00:00";
        String endTime = createTime + " 23:59:59";
        String idStr = "";
        if(null != id){
            idStr = String.valueOf(id);
        }
        List<StatisticsVisit> statisticsVisitList = statisticsVisitMapper.selectInfoByCompanyType(idStr ,String.valueOf(companyType),startTime,endTime,channel);
        return statisticsVisitList;
    }
}
